//编译 javac HelloHttp.java 得到class类
//运行测试 java HelloHttp
//打包为jar 1.创建manifest文件 echo -e "Main-Class: HelloHttp_j17\n" > manifest.txt 2. jar cfm hello_j17.jar manifest.txt *.class 3. 运行java -jar  hello_j17.jar 
import com.sun.net.httpserver.HttpServer;
import com.sun.net.httpserver.HttpHandler;
import com.sun.net.httpserver.HttpExchange;

import java.io.IOException;
import java.io.OutputStream;
import java.net.InetSocketAddress;

public class HelloHttp_j17 {
    public static void main(String[] args) throws IOException {
        HttpServer server = HttpServer.create(new InetSocketAddress(8017), 0);
        server.createContext("/", new HelloHandler());
        server.setExecutor(null); 
        server.start();
        System.out.println("Server started on http://localhost:8017/");
    }

    static class HelloHandler implements HttpHandler {
        public void handle(HttpExchange exchange) throws IOException {
            String response = "Hello, World (Java 17)";
            exchange.sendResponseHeaders(200, response.length());
            try (OutputStream os = exchange.getResponseBody()) {
                os.write(response.getBytes());
            }
        }
    }
}
